package com.ruoyi.jproom.service.impl;

import com.ruoyi.common.config.Global;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.jproom.entity.*;
import com.ruoyi.jproom.mapper.HousingEntrustMapper;
import com.ruoyi.jproom.mapper.ResidentialMapper;
import com.ruoyi.jproom.mapper.SellMapper;
import com.ruoyi.jproom.service.HousingEntrustService;
import com.ruoyi.jproom.service.ImageInfoService;
import com.ruoyi.jproom.utils.BaseResult;
import com.ruoyi.jproom.utils.ResultEnum;
import com.ruoyi.jproom.utils.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author zeo
 * @explain 房源服务实现类
 * @date 2020/7/27 11:08
 */
@Service
public class HousingEntrustServiceImpl implements HousingEntrustService {


    @Autowired
    HousingEntrustMapper housingEntrustMapper;

    @Autowired
    ResidentialMapper residentialMapper;

    @Autowired
    ImageInfoService imageInfoService;

    @Autowired
    SellMapper sellMapper;

    @Override
    public BaseResult insertMeSellEntrust(MeSellInfo meSellInfo) {
        meSellInfo.setMeSellId(Utils.getUUID());
        meSellInfo.setResidentialId(residentialMapper.findResidentialIdByResidentialName(meSellInfo.getResidentialName()));

        if (housingEntrustMapper.insertMeSellEntrust(meSellInfo) > 0) {
            return BaseResult.build(ResultEnum.SUCCESS);
        } else {
            return BaseResult.build(ResultEnum.FAILURE);
        }
    }

    @Override
    public BaseResult insertMeHaveRentEntrust(MeHaveRentInfo meHaveRentInfo) {
        meHaveRentInfo.setMeHaveRentId(Utils.getUUID());
        meHaveRentInfo.setResidentialId(residentialMapper.findResidentialIdByResidentialName(meHaveRentInfo.getResidentialName()));
        if (housingEntrustMapper.insertMeHaveRentEntrust(meHaveRentInfo) > 0) {
            return BaseResult.build(ResultEnum.SUCCESS);
        } else {
            return BaseResult.build(ResultEnum.FAILURE);
        }
    }

    @Override
    public List<MeSellInfo> findMeSellEntrustList() {
        return housingEntrustMapper.findMeSellEntrustList();
    }

    @Override
    public MeSellInfo findMeSellEntrustById(String meSellId) {
        return housingEntrustMapper.findMeSellEntrustById(meSellId);
    }


    @Override
    public List<MeHaveRentInfo> findMeHaveRentEntrustList() {
        return housingEntrustMapper.findMeHaveRentEntrustList();
    }

    @Override
    public MeHaveRentInfo findMeHaveRentInfoEntrustById(String meHaveRentId) {
        return housingEntrustMapper.findMeHaveRentInfoEntrustById(meHaveRentId);
    }

    @Override
    public BaseResult addHousingSell(MultipartFile[] residentialFile, String residentialId, String userId, int isResidential, Residential residential, Housing housing, MultipartFile[] housingFile, Sell sell) throws IOException {
        String Path = Global.getUploadPath();

        //生成房源UUID新增房源信息,并上传房源附件
        String housingUUID = Utils.getUUID();
        housing.setHousingId(housingUUID);
        housingEntrustMapper.addHousingSell(housing);
        for (MultipartFile file : housingFile) {
            if (!"".equals(file.getOriginalFilename())) {
                String filePath = FileUploadUtils.upload(Path, file);
                filePath = "http://" + Global.getTomcatAddress() + "/images" + filePath.substring(8, filePath.length());
                imageInfoService.addImageFile(new ImageInfo(filePath, housingUUID));
            }
        }

        if (isResidential == 0) {
            //生成小区UUID新增小区信息,并上传小区附件
            String residentialUUID = Utils.getUUID();
            residential.setResidentialId(residentialUUID);
            residentialMapper.addResidential(residential);
            System.out.println(residentialFile.length);
            for (MultipartFile file : residentialFile) {
                if (!"".equals(file.getOriginalFilename())) {
                    String filePath = FileUploadUtils.upload(Path, file);
                    filePath = "http://" + Global.getTomcatAddress() + "/images" + filePath.substring(8, filePath.length());
                    imageInfoService.addImageFile(new ImageInfo(filePath, residentialUUID));
                }
            }

            //形成关联并将标题以及标签插入到数据库售卖表中
            String sellUUID = Utils.getUUID();
            sellMapper.addSellInfo(new Sell(sellUUID, residentialUUID, housingUUID, sell.getTitle(), sell.getSellLabel(), sell.getUserId()));

            //将小区id补全到售房委托表中
            String meSellId = housing.getMeSellId();
            housingEntrustMapper.updateMeSellCompletion(meSellId, residentialUUID);
        } else {
            //直接房源并关联小区
            //形成关联并将标题以及标签插入到数据库售卖表中
            String sellUUID = Utils.getUUID();
            sellMapper.addSellInfo(new Sell(sellUUID, residentialId, housingUUID, sell.getTitle(), sell.getSellLabel(), sell.getUserId()));
            String meSellId = housing.getMeSellId();
            housingEntrustMapper.updateMeSellHandle(meSellId);
        }
        return BaseResult.build(ResultEnum.SUCCESS);
    }

    @Override
    public String findHousingIdMeSellEntrustId(String MeSellEntrustId) {
        return housingEntrustMapper.findHousingIdMeSellEntrustId(MeSellEntrustId);
    }

    @Override
    public List<Map<String, Object>> queryEntrustInfoList(Sell sell) {
        return housingEntrustMapper.queryEntrustInfoList(sell);
    }

    @Override
    public BaseResult addHousingRent(MultipartFile[] residentialFile, String residentialId, String userId, int isResidential, Residential residential, Housing housing, MultipartFile[] housingFile, Sell sell) throws IOException {
        String Path = Global.getUploadPath();

        //生成房源UUID新增房源信息,并上传房源附件
        String housingUUID = Utils.getUUID();
        housing.setHousingId(housingUUID);
        housingEntrustMapper.addHousingRent(housing);
        for (MultipartFile file : housingFile) {
            if (!"".equals(file.getOriginalFilename())) {
                String filePath = FileUploadUtils.upload(Path, file);
                filePath = "http://" + Global.getTomcatAddress() + "/images" + filePath.substring(8, filePath.length());
                imageInfoService.addImageFile(new ImageInfo(filePath, housingUUID));
            }
        }

        if (isResidential == 0) {
            //生成小区UUID新增小区信息,并上传小区附件
            String residentialUUID = Utils.getUUID();
            residential.setResidentialId(residentialUUID);
            residentialMapper.addResidential(residential);
            System.out.println(residentialFile.length);
            for (MultipartFile file : residentialFile) {
                if (!"".equals(file.getOriginalFilename())) {
                    String filePath = FileUploadUtils.upload(Path, file);
                    filePath = "http://" + Global.getTomcatAddress() + "/images" + filePath.substring(8, filePath.length());
                    imageInfoService.addImageFile(new ImageInfo(filePath, residentialUUID));
                }
            }


            //形成关联并将标题以及标签插入到数据库售卖表中
            String sellUUID = Utils.getUUID();
            sellMapper.addSellInfo(new Sell(sellUUID, residentialUUID, housingUUID, sell.getTitle(), sell.getSellLabel(), sell.getUserId()));

            //将小区id补全到售房委托表中
            String meHaveRentId = housing.getMeHaveRentId();
            housingEntrustMapper.updateMeHaveRentCompletion(meHaveRentId,residentialUUID);
        } else {
            //直接房源并关联小区
            //形成关联并将标题以及标签插入到数据库售卖表中
            String sellUUID = Utils.getUUID();
            sellMapper.addSellInfo(new Sell(sellUUID, residentialId, housingUUID, sell.getTitle(), sell.getSellLabel(), sell.getUserId()));
            String meHaveRentId = housing.getMeHaveRentId();
            housingEntrustMapper.updateMeHaveRentHandle(meHaveRentId);
        }
        return BaseResult.build(ResultEnum.SUCCESS);
    }

    @Override
    public String findHousingIdByMeHaveRentEntrustId(String MeHaveRentEntrustId) {
        return housingEntrustMapper.findHousingIdByMeHaveRentEntrustId(MeHaveRentEntrustId);
    }

    @Override
    public Housing findHousingById(String housingId) {
        Housing housing = housingEntrustMapper.findHousingById(housingId);
        housing.setImageInfoList(imageInfoService.findImageInfoListBySellId(housingId));
        return housing;
    }
}
